package cn.chenshiwu121.controller;

import cn.chenshiwu121.pojo.ContentCategory;
import cn.chenshiwu121.pojo.EasyUITreeNode;
import cn.chenshiwu121.pojo.TaotaoResult;
import cn.chenshiwu121.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/content/category")
public class ContentCategoryController {

    @Autowired
    private ContentCategoryService service;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public List<EasyUITreeNode> getContentCategoryList(@RequestParam(value = "id", defaultValue = "0") Long parentId) {
        return service.getContentCategoryList(parentId);
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult createContentCategory(ContentCategory category) {
        TaotaoResult result = null;
        try {
            result = TaotaoResult.ok(service.createContentCategory(category));
        } catch (Exception e) {
            result = TaotaoResult.build(1, "ERROR:create failed");
        }
        return result;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult updateContentCategory(ContentCategory category) {
        TaotaoResult result = null;
        try {
            service.updateContentCategory(category);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult deleteContentCategory(Long id) {
        TaotaoResult result = null;
        try {
            service.deleteContentCategory(id);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

}
